package web.protocols.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import domain.entities.*;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.ConcurrencyFailureException;
import services.CenterCRUDService;
import services.InvestigadorProtocoloCentroService;
import services.ProtocolCRUDService;
import services.RoleCRUDService;
import services.exceptions.NoProtocoloCompromisopaisSelectedException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

@Namespace("/Protocols/Sites")
public class ProtocolSitesInvestigatorController extends ActionSupport implements ModelDriven {

    private ProtocolCRUDService protocolCRUDService;
    private InvestigadorProtocoloCentroService investigadorProtocoloCentroService;
    private CenterCRUDService centerCRUDService;
    private RoleCRUDService roleCRUDService;
    private InvolucradoDAO involucradoDAO;
    private ProtocoloCentroDAO protocoloCentroDAO;
    private List<InvestigadorProtocoloCentro> list;
    private InvestigadorProtocoloCentro investigadorProtocoloCentro;
    private List<Centro> sites;
    private List<Involucrado> investigators;
    private boolean selected;
    private String siteScoreC;
    private Collection<ComiteEtica> comitesC = new ArrayList<ComiteEtica>();
    private List<Role> sitesRoles = new ArrayList<Role>();
    private String errorMessage;

    @Autowired
    public ProtocolSitesInvestigatorController(ProtocolCRUDService protocolCRUDService,
          InvestigadorProtocoloCentroService investigadorProtocoloCentroService,
          CenterCRUDService centerCRUDService, InvolucradoDAO involucradoDAO,
          ProtocoloCentroDAO protocoloCentroDAO, RoleCRUDService roleCRUDService) {
        this.protocolCRUDService = protocolCRUDService;
        this.investigadorProtocoloCentroService = investigadorProtocoloCentroService;
        this.investigadorProtocoloCentro = new InvestigadorProtocoloCentro();
        this.centerCRUDService = centerCRUDService;
        this.involucradoDAO = involucradoDAO;
        this.protocoloCentroDAO = protocoloCentroDAO;
        this.roleCRUDService = roleCRUDService;
    }

    @Action(value="main", results={
            @Result(name="success", location="/protocols.sites.tiles", type="tiles")
    })
    public String execute() throws NoProtocoloCompromisopaisSelectedException {
        ProtocoloCompromisopais protocoloCompromisopais = protocolCRUDService.recoverProtocoloCompromisopais();
        list = investigadorProtocoloCentroService.findAllBy(protocoloCompromisopais);

        if(investigadorProtocoloCentro.getId() != null &&
           investigadorProtocoloCentro.getId().getInvestigadorid() != null &&
           investigadorProtocoloCentro.getId().getProtocoloCentrocentroid() != null &&
           investigadorProtocoloCentro.getId().getProtocoloCentroprotocoloCompromisopaisprotocoloid() != null &&
           investigadorProtocoloCentro.getId().getProtocoloCentroprotocoloCompromisopaiscompromisopaisid() != null){
            //se edita la instancia
            investigadorProtocoloCentro = investigadorProtocoloCentroService.findById(investigadorProtocoloCentro.getId());
            if (investigadorProtocoloCentro.getStatus().equals(SiteState.Selected)){
                selected = true;
            }
            comitesC = new ArrayList<ComiteEtica>(investigadorProtocoloCentro.getProtocoloCentro().getComiteEticas());
        }
        else{
            investigadorProtocoloCentro = null;
        }

        return SUCCESS;
    }

    @Action(value="save", results={
            @Result(name="success",location="main", type="redirectAction"),
            @Result(name="input", location="/protocols.sites.tiles", type="tiles")
    })
    public String save() throws NoProtocoloCompromisopaisSelectedException {
        try {
            ProtocoloCompromisopais protocoloCompromisopais = protocolCRUDService.recoverProtocoloCompromisopais();

//            investigadorProtocoloCentroService.save(investigadorProtocoloCentro,
//                    protocoloCompromisopais,
//                    selected,
//                    comitesC);

        } catch (NoProtocoloCompromisopaisSelectedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (ConcurrencyFailureException cfe){
            LOG.error("error de " + investigadorProtocoloCentro.getJustification());
            errorMessage = "Someone has just saved the same record you did";
            execute();
            return INPUT;
        }
        return SUCCESS;
    }

    public List<InvestigadorProtocoloCentro> getList() {
        return list;
    }

    public void setList(List<InvestigadorProtocoloCentro> list) {
        this.list = list;
    }

    public InvestigadorProtocoloCentro getInvestigadorProtocoloCentro() {
        return investigadorProtocoloCentro;
    }

    public void setInvestigadorProtocoloCentro(InvestigadorProtocoloCentro investigadorProtocoloCentro) {
        this.investigadorProtocoloCentro = investigadorProtocoloCentro;
    }

    public List<Centro> getSites() {
        sites = centerCRUDService.list();
        return sites;
    }

    public void setSites(List<Centro> sites) {
        this.sites = sites;
    }

    public List<Involucrado> getInvestigators() {
        investigators = involucradoDAO.findAll();
        return investigators;
    }

    public void setInvestigators(List<Involucrado> investigators) {
        this.investigators = investigators;
    }

    @Override
    public Object getModel() {
        return investigadorProtocoloCentro;
    }

    public boolean getSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public Score[] getScores() {
        return Score.values();
    }

    public Collection<ComiteEtica> getComitesC() {
        return comitesC;
    }

    public void setComitesC(Collection<ComiteEtica> comitesC) {
        this.comitesC = comitesC;
    }

    public List<Role> getSitesRoles() {
        List<Role> list = roleCRUDService.findSiteRole();
        Iterator<Role> it = list.iterator();
        while(it.hasNext()){
            if(it.next().getDescripcion().equals("Investigator")){
                it.remove();
            }
        }
        return list;
    }

    public void setSitesRoles(List<Role> sitesRoles) {
        this.sitesRoles = sitesRoles;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }
}
